package com.djf.eg.util.convert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.djf.eg.entity.SysPermission;
import com.djf.eg.entity.SysRole;
import com.djf.eg.entity.UserInfo;
import com.djf.eg.vo.MenuVo;
import com.djf.eg.vo.PermissionVo;
import com.djf.eg.vo.RoleVo;
import com.djf.eg.vo.UserInfoVo;

public class Entity2Vo {
	public static UserInfoVo userEntity2Vo (UserInfo userInfo) {
		UserInfoVo vo = new UserInfoVo();
		vo.setId(userInfo.getId());
		vo.setUsername(userInfo.getUsername());
		vo.setNickname(userInfo.getNickname());
		vo.setPassword(userInfo.getPassword());
		vo.setCreateTime(userInfo.getCreateTime());
		vo.setUpdateTime(userInfo.getUpdateTime());
		vo.setDeleteStatus(userInfo.getDeleteStatus());
		vo.setRoles(userInfo.getRoles());
		vo.setMenus();
		vo.setPermissions();
		return vo;
	}
	
	public static RoleVo roleEntity2Vo (SysRole role) {
		RoleVo vo = new RoleVo();
		vo.setId(role.getId());
		vo.setRoleName(role.getRoleName());
		vo.setCreateTime(role.getCreateTime());
		vo.setUpdateTime(role.getUpdateTime());
		vo.setPermissions(role.getPermissions());
		vo.setDeleteStatus(role.getDeleteStatus());
//		vo.setUserInfos(role.getUserInfos());
		List<SysPermission> permissionList = role.getPermissions();
		Map<String, MenuVo> menuMap = new HashMap<String, MenuVo>();
		Set<String> menuSet = new HashSet<String>();
		for (SysPermission permission : permissionList) {
			String menuCode = permission.getMenuCode();
			if (!menuSet.contains(menuCode)) {
				menuSet.add(menuCode);
				MenuVo menuVo = new MenuVo();
				menuVo.setMenuCode(menuCode);
				menuVo.setMenuName(permission.getMenuName());
				List<PermissionVo> permissionVoList = new ArrayList<PermissionVo>();
				PermissionVo permissionVo = new PermissionVo();
				permissionVo.setPermissionCode(permission.getPermissionCode());
				permissionVo.setPermissionName(permission.getPermissionName());
				permissionVoList.add(permissionVo);
				menuVo.setPermissions(permissionVoList);
				menuMap.put(menuCode, menuVo);
			} else {
				PermissionVo permissionVo = new PermissionVo();
				permissionVo.setPermissionCode(permission.getPermissionCode());
				permissionVo.setPermissionName(permission.getPermissionName());
				
				MenuVo menuVo = menuMap.get(menuCode);
				List<PermissionVo> permissionVoList = menuVo.getPermissions();
				permissionVoList.add(permissionVo);
				menuVo.setPermissions(permissionVoList);
			}
		}
		Iterator<String> it = menuMap.keySet().iterator();
		List<MenuVo> menuVoList = new ArrayList<MenuVo>();
		while(it.hasNext()) {
			MenuVo menuVo = menuMap.get(it.next());
			menuVoList.add(menuVo);
		}
		vo.setMenus(menuVoList);
		return vo;
	}
	
	public static List<MenuVo> permissionList2MenuVoList (List<SysPermission> permissionList) {
		Map<String, MenuVo> menuMap = new HashMap<String, MenuVo>();
		Set<String> menuSet = new HashSet<String>();
		for (SysPermission permission : permissionList) {
			String menuCode = permission.getMenuCode();
			if (!menuSet.contains(menuCode)) {
				menuSet.add(menuCode);
				MenuVo menuVo = new MenuVo();
				menuVo.setMenuCode(menuCode);
				menuVo.setMenuName(permission.getMenuName());
				List<PermissionVo> permissionVoList = new ArrayList<PermissionVo>();
				PermissionVo permissionVo = new PermissionVo();
				permissionVo.setId(permission.getId());
				permissionVo.setPermissionCode(permission.getPermissionCode());
				permissionVo.setPermissionName(permission.getPermissionName());
				permissionVoList.add(permissionVo);
				menuVo.setPermissions(permissionVoList);
				menuMap.put(menuCode, menuVo);
			} else {
				PermissionVo permissionVo = new PermissionVo();
				permissionVo.setId(permission.getId());
				permissionVo.setPermissionCode(permission.getPermissionCode());
				permissionVo.setPermissionName(permission.getPermissionName());
				
				MenuVo menuVo = menuMap.get(menuCode);
				List<PermissionVo> permissionVoList = menuVo.getPermissions();
				permissionVoList.add(permissionVo);
				menuVo.setPermissions(permissionVoList);
			}
		}
		Iterator<String> it = menuMap.keySet().iterator();
		List<MenuVo> menuVoList = new ArrayList<MenuVo>();
		while(it.hasNext()) {
			MenuVo menuVo = menuMap.get(it.next());
			menuVoList.add(menuVo);
		}
		return menuVoList;
	}
}
